// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui';
import VueRouter from 'vue-router'
import 'element-ui/lib/theme-chalk/index.css';
import '../theme/index.css'
import axios from "axios";
import VueAxios from 'vue-axios';
import JsonViewer from 'vue-json-viewer'

axios.defaults.baseURL = "https://cdz-log.test.senthink.com";
// 请求拦截
axios.interceptors.request.use(config => {
    if(localStorage.token) {
		let token = localStorage.token;
        config.headers.Authorization = 'Bearer '+token;
    }
    return config
}, error => {
	return Promise.reject(error)
})
// 响应拦截
axios.interceptors.response.use(response => {
    return response
}, error => {
    const status = error.response;
    if(status === 401) {
        console.log("测试token过期")
        localStorage.removeItem('token');
        this.$router.push('/login')
    }
    return Promise.reject(error)
})
export default axios;

Vue.config.productionTip = false
Vue.use(ElementUI);
Vue.use(VueRouter);
Vue.use(VueAxios, axios);
Vue.use(JsonViewer)

router.beforeEach((to,from,next) => {
    if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
        if (localStorage.token) {  // 通过vuex state获取当前的token是否存在
            next();
        }
        else {
            next('/login')
        }
    }
    else {
        next();
    }
  })

/* eslint-disable no-new */
new Vue({
	// el: '#app',
	router,
	render: h => h(App)
	// components: { App },
	// template: '<App/>'
}).$mount('#app')
